package root.electronclass.electronclass.school

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.common.util.HeadUtil
import root.electronclass.electronclass.databinding.ActivitySchoolDirectorBinding
import root.electronclass.electronclass.databinding.ItemItemSchoolDircetorBinding
import root.electronclass.electronclass.databinding.ItemSchoolDirectorActivityBinding
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.mvp.entity.FirstCharTeacherDto
import root.electronclass.electronclass.mvp.entity.TeacherVO
import root.electronclass.electronclass.school.model.SchoolNotificationModel

class SchoolDirectorFragment : ViewBindingFragment<ActivitySchoolDirectorBinding>() {
    private val viewModel: SchoolNotificationModel by viewModels()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        activity?.setTitle("教职工风采")
        binding.search.doOnSearch {
            viewModel.listByFirstChar(it)
            false
        }
        binding.recyclerView2.adapter = adapter
        viewModel.listByFirstChar.observe(viewLifecycleOwner){ its ->
            adapter.notifyChanged(its)
        }
        viewModel.listByFirstChar("")
    }

    private val adapter = BasicAdapter<FirstCharTeacherDto, ItemSchoolDirectorActivityBinding> { holder, item ->
        holder.binding.itemHeader.text = item.firstChar
        val childAdapter = BasicAdapter<TeacherVO, ItemItemSchoolDircetorBinding> { holder1, childItem ->

            if (childItem.userInfoVo.facePhotoFormat.isNullOrEmpty()) {
                holder1.binding.headIv.setActualImageResource(HeadUtil.getHeadPic(if (childItem.userInfoVo.gender == 1) 1 else 0))
            } else {
                if (childItem.userInfoVo.facePhotoFormat.orEmpty()[0].url.isNullOrEmpty()){
                    holder1.binding.headIv.setActualImageResource(HeadUtil.getHeadPic(if (childItem.userInfoVo.gender == 1) 1 else 0))
                }else {
                    holder1.binding.headIv.setImageURI(childItem.userInfoVo.facePhotoFormat.orEmpty()[0].url)
                }
            }
            holder1.binding.name.text = childItem.name
            holder1.binding.desc.text = childItem.userInfoVo.positionName ?: ""
        }
        holder.binding.itemRecycler.layoutManager = LinearLayoutManager(activity, RecyclerView.HORIZONTAL,false)
        holder.binding.itemRecycler.adapter = childAdapter
        childAdapter.refresh(item.teachers)
    }


}